package com.example.demo.filter;/**
 * Created by kete on 2018-11-26.
 */

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import io.netty.handler.timeout.IdleStateHandler;
import org.testng.Assert;

import java.util.HashSet;
import java.util.Set;

/**
 * 作者：kete
 * 创建时间：2018-11-26 17:08
 * 功能描述：
 * 版本：
 */
public class FileterTest {

    public void test1(){

        long star = System.currentTimeMillis();
        BloomFilters bloomFilters = new BloomFilters(10000000) ;
        for (int i = 0; i < 10000000; i++) {
            bloomFilters.add(i + "") ;
        }
        Assert.assertTrue(bloomFilters.check(1+""));
        Assert.assertTrue(bloomFilters.check(2222+""));
        Assert.assertTrue(bloomFilters.check(33333+""));
        Assert.assertTrue(bloomFilters.check(999999+""));
        Assert.assertFalse(bloomFilters.check(400230340+""));
        long end = System.currentTimeMillis();
        System.out.println("执行时间：" + (end - star));

    }
    public void test2(){

        long star = System.currentTimeMillis();

        Set<Integer> hashset = new HashSet<>(10000000) ;
        for (int i = 0; i < 10000000; i++) {
            hashset.add(i) ;
        }
        Assert.assertTrue(hashset.contains(1));
        Assert.assertTrue(hashset.contains(2));
        Assert.assertTrue(hashset.contains(3));

        long end = System.currentTimeMillis();
        System.out.println("执行时间：" + (end - star));

    }
    public void test3(){
        long star = System.currentTimeMillis();
        BloomFilter<Integer> filter = BloomFilter.create(
                Funnels.integerFunnel(),
                10000000,
                0.01);

        for (int i = 0; i < 10000000; i++) {
            filter.put(i);
        }

        Assert.assertTrue(filter.mightContain(1));
        Assert.assertTrue(filter.mightContain(2));
        Assert.assertTrue(filter.mightContain(3));
        Assert.assertFalse(filter.mightContain(20000000));
        long end = System.currentTimeMillis();
        System.out.println("执行时间：" + (end - star));

    }

    public static void main(String[] args) {
        FileterTest test = new FileterTest();
        test.test3();
    }
}
